// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from './store'
import router from './router'
import fastClick from 'fastclick'
import { ToastPlugin, ConfirmPlugin } from 'vux'
import './common/styles/reset.styl'
import './common/styles/iconfont.styl'
import './common/styles/base.styl'

Vue.config.productionTip = false
fastClick.attach(document.body)

// vux插件
Vue.use(ToastPlugin)
Vue.use(ConfirmPlugin)

router.beforeEach((to, from, next) => {
  // 路由发生变化修改页面title
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()

  // 当访问login页面时判断是否已登陆
  if (to.path === '/login') {
    if (localStorage.getItem('mzjShopInfo')) {
      next({ path: '/index' })
      Vue.$vux.toast.text('您已登录！')
    }
  }
  next()

  if (to.matched.some(m => m.meta.auth)) {
    if (localStorage.getItem('mzjShopInfo')) {
      next()
    } else if (to.path !== '/') {
      next({ path: '/login' })
      Vue.$vux.toast.text('请先登录！')
    }
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  components: { App },
  template: '<App/>'
})
